<template>
	<view>
		<view class="content u-page">
			<!-- search -->
			<view class="search-content">
				<view class="search" @click="toSearch">
					<view class="icon">
						<u-icon name="search" color="#B0B3BF" size="28"></u-icon>
					</view>
					<view class="word">
						<text>请输入课程名称~</text>
					</view>
				</view>
			</view>
			<!-- 垂直分类 -->
			<view class="u-menu-wrap">
				<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
					<view v-for="(item,index) in navbar" :key="index" class="u-tab-item"
						:class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
						<text class="u-line-1">{{item.name}}</text>
					</view>
				</scroll-view>
				<view class="page-view">
					<cate-ad></cate-ad>
					<!-- <u-section title="php" sub-title="查看更多" :show-line="showLine" v-show="current == 0"></u-section> -->
					<view class="goods-content" v-if="!showNothing">
						<view class="cate-card" v-for="(item,index) in goodsList" :key="index" @click="toDetail(item._id,item.teacher_id)">
							<view class="left">
								<view class="title">
									<text>{{item.name}}</text>
								</view>
								<view class="bottom">
									<view class="left">
										<text>￥{{item.sale_price}}</text>
									</view>
									<view class="right">
										<text>{{item.total_sell_count}}人付费</text>
									</view>
								</view>
							</view>
							<view class="right">
								<image :src="item.main_pic" mode=""></image>
							</view>
						</view>
					</view>
					<u-empty text="暂无数据" mode="list" v-if="showNothing"></u-empty>
				</view>
			</view>
		</view>
		<!-- tabbar -->
		<u-tabbar v-model="currentTab" :list="tabbar"></u-tabbar>
	</view>
</template>

<script>
	import {
		tabbar
	} from '@/common/tabbar.js'
	import cateAd from './components/cate-ad.vue'
	export default {
		data() {
			return {
				currentTab: 1,
				tabbar: tabbar,
				//search
				keyword: '',
				showAction: false,
				inputAlign: 'center',
				//垂直分类部分
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				navbar: [],//分类列表
				goodsList:[],//商品列表
				showNothing:false,
				showLine: false
			}
		},
		components: {
			cateAd
		},
		onLoad() {
			this.getCategory();
		},
		watch:{
			current(newVal,oldVal){
				console.log(newVal);
				this.getGoods(this.navbar[newVal]._id)
			}
		},
		methods: {
			toSearch(){
				uni.navigateTo({
					url:'../search/search'
				})
			},
			//垂直分类的方法开始
			// 点击左边的栏目切换
			swichMenu(index) {
				this.current = index
			},

			//垂直分类的方法结束
			getCategory(){
				const db = uniCloud.database()
				db.collection('opendb-mall-categories')
					.field('name,_id')
					.get()
					.then(res => {
						console.log('cate', res);
						if(res.result.data.length != 0){
							this.navbar = res.result.data;
							this.getGoods(this.navbar[0]._id)
						}
					}).catch(err => {
						console.error(err)
					})
			},
			getGoods(cateId){
				const db = uniCloud.database()
				db.collection('opendb-mall-goods')
					.where("category_id=='"+ cateId +"'")
					.field('name,sale_price,total_sell_count,main_pic,teacher_id')
					.get()
					.then(res => {
						console.log('goods', res);
						if(res.result.data.length != 0){
							this.showNothing = false;
							this.goodsList = res.result.data;
						}else{
							this.showNothing = true;
						}
						
					}).catch(err => {
						console.error(err)
					})
			},
			toDetail(id, tid) {
				console.log(id, tid);
				uni.navigateTo({
					url: '../course-detail/course-detail?courseId=' + id + "&tid=" + tid
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.search-content{
			padding: 25rpx;
			.search{
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				width: 480rpx;
				height: 55rpx;
				background-color: #F7F8FA;
				border-radius: 35rpx;
				padding: 10rpx 0 10rpx 220rpx;
				.icon{
					
				}
				.word{
					color: #B0B3BF;
					font-size: 28rpx;
				}
			}
		}

		//垂直分类
		.u-menu-wrap {
			background-color: #F5F5F5;
			flex: 1;
			display: flex;
			overflow: hidden;
			min-height: 1100rpx;
		}

		.u-tab-view {
			height: 100%;
			background: #f6f6f6;
		}

		.u-tab-item {
			height: 110rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			color: #666666;
			font-weight: 400;
			line-height: 1;
			padding-left: 6rpx;
			text-align: center;
			background-color: #f6f6f6;

			image {
				width: 30rpx;
				height: 30rpx;
			}
		}

		.u-tab-item-active {
			position: relative;
			color: #18BF98;
			background: #fff;
			border-left: 2px solid #18BF98;

			&:before {
				content: "";
				position: absolute;
				height: 32rpx;
				left: 0;
				top: 39rpx;
			}
		}

		.page-view {
			padding: 15rpx;
			background-color: #fff;
		}

	}
	//goods-content
	.goods-content{
		.cate-card{
			width: 540rpx;
			height: 160rpx;
			display: flex;
			justify-content: space-between;
			padding: 20rpx 0;
			border-bottom: 1px solid #e1e1e1;
			.left{
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				width: 280rpx;
				height: 160rpx;
				.title{
					font-size: 28rpx;
					font-weight: bold;
					color: #555555;
					margin-bottom: 40rpx;
				}
				.bottom{
					display: flex;
					justify-content: space-between;
					.left{
						font-size: 28rpx;
						color: #F56C6C;
						font-weight: bold;
					}
					.right{
						font-size: 24rpx;
						color: #B5B5B5;
					}
				}
			}
			.right{
				width: 240rpx;
				height: 160rpx;
				border-radius: 10rpx;
				overflow: hidden;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
	}
</style>
